package bdz.projects.cbt.ui
{
	import bdz.projects.cbt.core.CBTInventory;
	import bdz.projects.cbt.core.ProductCollection;
	import bdz.projects.cbt.events.CBTGameEvent;
	import bdz.projects.cbt.events.CBTUIEvent;
	
	import flash.events.MouseEvent;
	
	public class CBTInventoryList extends CBTList {
		
		
		private var _listref:CBTInventory;
		public function CBTInventoryList() {
			
			_renderer = "InventoryItemRenderer";
			super();
			_channel.addEventListener(CBTGameEvent.GAME_CHANGE, onGameChanged);
			_channel.addEventListener(CBTGameEvent.ITEM_BOUGHT, onGameChanged);
			_channel.addEventListener(CBTGameEvent.ITEM_SOLD, onGameChanged);
		}
		override protected function onGameChanged(evt:CBTGameEvent):void{
			_listref = evt.data.inventory;
			dataProvider = evt.data.inventory.productList;
		}
		override protected function onDoubleClickItem(evt:MouseEvent):void{
			super.onDoubleClickItem(evt);
			_channel.dispatchEvent(new CBTUIEvent(CBTUIEvent.INVENTORY_ITEM_SELECTED, {type:"inventory", lastSelected:_lastSelected, currentSelected:evt.target, info:_lastSelected.info}));
			_channel.dispatchEvent(new CBTUIEvent(CBTUIEvent.SHOW_SELL_INTERFACE));
		}
		override protected function onSelectItem(evt:MouseEvent):void{
			super.onSelectItem(evt);
			_channel.dispatchEvent(new CBTUIEvent(CBTUIEvent.INVENTORY_ITEM_SELECTED, {type:"inventory", lastSelected:_lastSelected, currentSelected:evt.target, info:_lastSelected.info}));
		}
		public function removeSomeProduct(al:String, count:Number):void{
			_listref.removeSomeProduct(al, count);
			dataProvider = _listref.productList;
		}
		public function removeAllProducts(al:String):void{
			_listref.removeAllProducts(al);
			dataProvider = _listref.productList;
		}
		public function findItemCount(al:String):Number{
			for each(var pc:ProductCollection in dataProvider){
				
				if(pc.alias == al){
					return pc.length;
				}
			}
			return 0;
		}
		public function totalItemCount():Number{
			var count:Number = 0;
			for each(var pc:ProductCollection in dataProvider){
				
				try{
					count += pc.length;
				}catch(e:Error){
					
				}
			}
			return count;
		}
	}
}